﻿using System;
using System.Linq;
using Microsoft.Practices.ServiceLocation;
using NCommon.Data;
using NCommon.Events;
using MyPetShop.Domain;

namespace MyPetShop.Domain
{
	/// <summary>
	/// Description of OrderCreateEventHandler.
	/// </summary>
    public class OrderCreateEventHandler : Handles<OrderCreateEvent>
	{
		public void Handle(OrderCreateEvent @event)
		{
			IRepository<Item> itemRepository = ServiceLocator.Current.GetInstance<IRepository<Item>>();
			
			foreach(OrderDetail detail in @event.Order.OrderDetail)
			{
				var item = itemRepository.Single(s=>s.ItemId == detail.ItemId);
				item.Quantity -= detail.Quantity;
			}
		}
	}
}
